<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Material"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	MaterialDAO materialDao = (MaterialDAO)ctx.getBean("MaterialDAO");
	request.setCharacterEncoding("utf-8");
	String mid = request.getParameter("mid");
	int tid = (Integer)session.getAttribute("tid");
	int id = 0;
	if(mid!=null || !"".equals(mid)){
		id = Integer.parseInt(mid);
	}
	String message = "";
	
	String type = request.getParameter("type");
	//type=2提交表单
	if(type!=null && (type=="2" || "2".equalsIgnoreCase(type))){
		//System.out.println("提交");
		request.setAttribute("type",2);
		String code = request.getParameter("code");
		String name = request.getParameter("codename");
		String codecompany = request.getParameter("codecompany");
		
		Material material = new Material();
		material = materialDao.findById(id);
		//判断登录名是否为空，是否重复
		if(code==null || "".equals(code)){
			message = "修改物资编码关联数据失败，物资编码不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			//System.out.println(message+"========"+stitleint+"========="+sgroupint+"=====type==="+type);
			pageContext.forward("/vm/materials/modMaterials.vm");
			return;
		}else{
			if((code == material.getCode() || code.equals(material.getCode())) && tid==material.getTeamId() && 0==material.getIsDelete()){
				message = "管理员名称未做修改！";
			}else{
				
				List md = materialDao.findAndPropertyDel(code,tid);
				if(md != null && md.size() > 0 ){
					message = "修改物资编码关联数据失败，物资编码已经存在。";
					request.setAttribute("suess",false);
					request.setAttribute("message",message);
					pageContext.forward("/vm/materials/modMaterials.vm");
					return;
				}
			}
		}
		
		//名称不可为空
		if(name==null || "".equals(name)){
			message = "修改物资编码关联数据失败，物资名称不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/materials/modMaterials.vm");
			return;
		}else{
			if((name == material.getName() || name.equals(material.getName())) && tid==material.getTeamId() && 0==material.getIsDelete()){
				message = "物资名称未做修改！";
			}else{
				
				List md = materialDao.findAndPropertyDel(name,tid);
				if(md != null && md.size() > 0 ){
					message = "修改物资编码关联数据失败，物资名称已经存在。";
					request.setAttribute("suess",false);
					request.setAttribute("message",message);
					pageContext.forward("/vm/materials/modMaterials.vm");
					return;
				}
			}
		}
		
		
		
		if(codecompany==null || "".equals(codecompany)){
			message = "修改物资编码关联数据失败，物资单位不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/materials/modMaterials.vm");
			return;
		}
		
		material.setCode(code);
		material.setCompany(codecompany);
		material.setName(name);
		materialDao.attachDirty(material);
		int nuid = material.getId();
		//System.out.println(uid);
		message = "恭喜您！修改物资编码关联数据成功！";
		request.setAttribute("message",message);
		request.setAttribute("suess",true);
		request.setAttribute("mid",nuid);
		pageContext.forward("/vm/materials/modMaterials.vm");
		return;
		
		
	}else{
	//正常显示
		//System.out.println("显示");
		Material material = new Material();
		material = materialDao.findById(id);
		
		request.setAttribute("material",material);
		request.setAttribute("type",1);
		request.setAttribute("mid",id);
		pageContext.forward("/vm/materials/modMaterials.vm");
		
	}
	
%>
